package com.ltu.rest.ippbx;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import com.alibaba.fastjson.JSONObject;
import com.ltu.enums.SysDict;
import com.ltu.rest.ThirdPartyHeader;
import com.ltu.util.exception.ApiException;
import com.ltu.util.http.HTTPMethod;
import com.ltu.util.http.HttpClientUtil2;
import com.ltu.util.http.HttpPostUtil;
import com.ltu.util.http.HttpRequestUtil;
import com.ltu.util.http.ReqParams;
import com.ltu.util.redis.RedistUtil;

import cn.hutool.core.date.DateUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@Component
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class IppbxRestServiceImpl  implements IppbxRestService,ThirdPartyHeader{
	private final RedistUtil redistUtil;
	private final RestTemplate restTemplate;
	private final HttpRequestUtil httpRequestUtil;
	private final SysDict.ThirdPartyDict dict = SysDict.ThirdPartyDict.IPPBX;
	
	//设置请求头
	@Override
	public  void setRestHeader(HttpRequest request) {
//		Map<String,String> headers = new HashMap<String, String>(4);
		request.getHeaders().add("Content-Type", "application/json");
		TokenVO token = getToken();
		request.getHeaders().add("Authorization", token.getSign());
	}
	
	
	private TokenVO  getToken() {
		TokenVO token = null;
			Object val = redistUtil.getValue(TokenPrefix);
		if(val == null) 
			token =initialToken();
		else
			token = (TokenVO) val;
		return token;
	}
	
	private TokenVO initialToken() {
		String url = dict.getPact().concat(Login);
		JSONObject  param = new JSONObject();
		param.put("username", UserName);
		param.put("password", Password);
		String resultStr="";
		TokenVO tokenVO =null;
		try {
			ReqParams  req =  new ReqParams();
			req.addHeader("Content-Type", "application/json");
			req.setPostText( param.toJSONString());
//			resultStr = HttpClientUtil2.sendHTTPRequest(url,req,HTTPMethod.METHOD_POST);
			resultStr = httpRequestUtil.post(url,param.toJSONString());
			log.error("登录IPPBX系统结果：{}",resultStr);
			JSONObject result = JSONObject.parseObject(resultStr);
			if(result.getInteger("code") != 0)
				log.error("登录IPPBX系统异常:{}",result.toString());
			String  token = result.getString("data");
			 tokenVO =	new TokenVO(token, DateUtil.formatDateTime(new Date()));
			redistUtil.setValue(TokenPrefix, tokenVO,1437l);//29天后过期
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return tokenVO;
	}

	/**
	 * @Description: 拨号
	 * @author 李生
	 * @date 2024-12-02 12:25:09 
	 * @param src 主叫号码
	 * @param dest 被叫号码
	 * @return
	 */
	@Override
	public JSONObject callDial(String src,String dest){
		String url = dict.getPact().concat(CallDial);
		JSONObject  param = new JSONObject();
		param.put("dest", dest);//被叫号码
		param.put("src", src);//主叫号码
		
		JSONObject result= null;
		try {
			result = restTemplate.postForObject(url, param, JSONObject.class);
		} catch (Exception e) {
			// TODO: handle exception
			redistUtil.removeKey(TokenPrefix);//29天后过期
			e.printStackTrace();
		}
		
		log.error("拨号结果：{}",result.toString());
		if(result.getInteger("code") != 0)
			log.error("IPPBX系统拨号异常:{}",result.toString());
		return result.getJSONObject("data");
	}



	
}
